package top.yokey.android.activity;

import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;

import top.yokey.android.R;
import top.yokey.android.base.App;
import top.yokey.android.base.BaseActivity;
import top.yokey.android.http.BaseCallBack;
import top.yokey.android.http.HttpHelp;

public class ModifyNameActivity extends BaseActivity {

    private AppCompatImageView backImageView;
    private AppCompatEditText newNameEditText;
    private AppCompatTextView modifyTextView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_modify_name);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initView() {

        backImageView = findViewById(R.id.backImageView);
        newNameEditText = findViewById(R.id.nameEditText);
        modifyTextView = findViewById(R.id.modifyTextView);

        newNameEditText.setText(App.get().getMemberBean().getMemberNickname());
        newNameEditText.setSelection(newNameEditText.getText().length());

    }

    @Override
    public void initEvent() {

        backImageView.setOnClickListener(view -> finish());

        modifyTextView.setOnClickListener(view -> {
            String newName = newNameEditText.getText().toString();
            if (newName.length() < 4) {
                Toast.makeText(this, "新昵称的长度不能小于4位", Toast.LENGTH_SHORT).show();
                return;
            }
            modifyTextView.setEnabled(false);
            modifyTextView.setText("正在修改昵称...");
            HttpHelp.get().ready("member", "modifyNickname")
                    .add("new_name", newName)
                    .get(new BaseCallBack() {
                        @Override
                        public void onSuccess(String data) {
                            App.get().getMemberBean().setMemberNickname(newName);
                            Toast.makeText(ModifyNameActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }

                        @Override
                        public void onError(String reason) {
                            Toast.makeText(ModifyNameActivity.this, reason, Toast.LENGTH_SHORT).show();
                            modifyTextView.setEnabled(true);
                            modifyTextView.setText("修改昵称");
                        }
                    });
        });

    }

}
